package com.canbot.u05.activity.vipguide;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.MainApplication;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.adapter.UsherModeChooseAudioListViewAdapter;
import com.canbot.u05.bean.Audio;
import com.canbot.u05.bean.JsonResponse;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by cwf on 2017/5/26.
 * 贵宾引导选择音频界面
 */

public class VipGuideChooseVoiceActivity extends BaseActivityNoTitle implements View.OnClickListener, AdapterView.OnItemClickListener {

        private ImageView imageViewback;

        private ListView listViewChooseVoice;

        private ImageView chooseVoiceCancle;

        private ImageView chooseVoiceSure;

        private UsherModeChooseAudioListViewAdapter adapter;

        private int lastPosition = -1;

        private Audio selectAudio;  //选择的音频

        private List<Audio> data = new ArrayList<>();

        private Dialog dialogLoading;

        private String isSetting;

        private String content;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_vipguide_choose_voice);
                setViews();

                isSetting = getIntent().getStringExtra("isSetting");
                content=getIntent().getStringExtra("content");
                getData();
        }

        private void setViews() {
                imageViewback = (ImageView) findViewById(R.id.imageView_back);
                listViewChooseVoice = (ListView) findViewById(R.id.lv_vipguide_choose_voice);
                chooseVoiceCancle = (ImageView) findViewById(R.id.iv_choose_voice_cancle);
                chooseVoiceSure = (ImageView) findViewById(R.id.iv_choose_voice_sure);
                imageViewback.setOnClickListener(this);
                chooseVoiceCancle.setOnClickListener(this);
                chooseVoiceSure.setOnClickListener(this);
                listViewChooseVoice.setOnItemClickListener(this);
        }

        private void getData() {
                dialogLoading = LoadingDialogUtils.createLoadingDialog(VipGuideChooseVoiceActivity.this, getResources().getString(R.string.request));
                String url = "http://" + IpMsgConst.SOCKET_HOST + ":7755/getAudioList";
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("path", "/uusound");
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                final JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
                                Logger.e("save", "onSuccess:" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/" + "strMsgBean.getRc()" + strMsgBean.getRc());

                                if (strMsgBean.getRc() == 0) {
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        dialogLoading.setCancelable(true);
                                                        cancelDialog();
                                                        data = JSON.parseArray(strMsgBean.getResult().toString(), Audio.class);
                                                        if(TextUtils.isEmpty(isSetting) && !TextUtils.isEmpty(content)){

                                                                for (int i = 0; i < data.size(); i++) {
                                                                        String path = data.get(i).getPath();
                                                                        if (path.contains(content)) {
                                                                                data.get(i).setChecked(true);
                                                                                lastPosition=i;
                                                                                break;
                                                                        }
                                                                }
                                                        }
                                                        setAdapter(data);
                                                        adapter.notifyDataSetChanged();
                                                }
                                        });
                                }


                        }

                        @Override
                        public void onFailure() {
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                cancelDialog();
                                                ToastTools.newInstance(VipGuideChooseVoiceActivity.this).showText(getString(R.string.network_error));
                                        }
                                });
                        }
                });


        }

        private void setAdapter(List<Audio> a) {
                adapter = new UsherModeChooseAudioListViewAdapter(VipGuideChooseVoiceActivity.this, a);
                listViewChooseVoice.setAdapter(adapter);
        }


        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.imageView_back:
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;
                        case R.id.iv_choose_voice_cancle:
                                //取消
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;
                        case R.id.iv_choose_voice_sure: //上传音频路径
                                //确定
                                if (selectAudio == null) {
                                        ToastTools.newInstance(VipGuideChooseVoiceActivity.this).showText(getString(R.string.please_select_audio));
                                        return;
                                }

                                showHint(selectAudio.getPath());

                                break;

                        default:
                                break;
                }
        }


        /**
         *修改自定义欢迎语
         * */
        public void showHint(String content) {

                dialogLoading = LoadingDialogUtils.createLoadingDialog(VipGuideChooseVoiceActivity.this, getResources().getString(R.string.request));

                HashMap<String, Object> paramsMap = new HashMap<String, Object>();

                //音频
                paramsMap.put("vipType", "updateRoute");
                paramsMap.put("identification", getIntent().getStringExtra("identification"));
                paramsMap.put("voicetype", 2 + "");
                paramsMap.put("content", content);//guide_reach_voice

                OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST2 + ":7755/" + "routevipGuide", paramsMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                if(!TextUtils.isEmpty(isSetting)){
                                        startActivity(new Intent(VipGuideChooseVoiceActivity.this, ReceptionSettingActivity.class)
                                                .putExtra("isSetting", isSetting)
                                                .putExtra("identification",getIntent().getStringExtra("identification"))
                                                .putExtra("isChoose","isChoose"));
                                }else{
                                        MainApplication.finishActivity(WelcomeSettingActivity.class.getName());
                                }
                                finish();
                        }

                        @Override
                        public void onFailure() {
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                cancelDialog();
                                                ToastTools.newInstance(VipGuideChooseVoiceActivity.this).showText(getString(R.string.network_error));
                                        }
                                });
                        }
                });

        }

        /**
         * 取消加载的Dialog
         */
        private void cancelDialog() {
                if (dialogLoading != null) {
                        if (dialogLoading.isShowing()) {
                                dialogLoading.cancel();
                        }
                }
        }

        @Override
        protected void onPause() {
                super.onPause();
                cancelDialog();
                MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stop_sound");
        }

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                if (data.get(position).isChecked()) {
                        data.get(position).setChecked(false);
                        adapter.updataView(position, listViewChooseVoice);
                        selectAudio = null;
                        MsgSendUtils.sendStringMsg(MsgType.SEND_3288_MEETING_VIDEO_PLAYING, "");
                }
                else {
                        data.get(position).setChecked(true);
                        adapter.updataView(position, listViewChooseVoice);
                        selectAudio = data.get(position);
                        String audioPath = selectAudio.getPath();
                        MsgSendUtils.sendStringMsg(MsgType.SEND_3288_MEETING_VIDEO_PLAYING, audioPath);
                }
                if (lastPosition != -1 && data.get(lastPosition).isChecked() && lastPosition != position) {
                        data.get(lastPosition).setChecked(false);
                        adapter.updataView(lastPosition, listViewChooseVoice);
                }
                lastPosition = position;

                Logger.e("123456",data.get(position).getName());
                MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, "/uusound/"+data.get(position).getName());
        }

}
